// packagePay/pages/insurance-organize-edit/insurance-organize-edit.js

// import { Console } from 'console'
import { addInsuranceEnterprise,insuranceWesureInfo,deleteInsureEnterprise } from '../../api/insurance'
import { confirm,toast,dateFormat } from '../../../utils/util'
import user from '../../../utils/user'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        companyName:'',
        day:'',
        night:'',
        id:'',
        addTimeList:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options){
        const eventChannel = this.getOpenerEventChannel()
        await eventChannel.on('acceptDataFromOpenerPage', (data)=>{
            console.log('data',data)
            this.setData({
                companyName:data.name,
                id:data.id,
            })
        })
        insuranceWesureInfo({
            id:options.id
        }).then(res=>{
            if(res.wesureTimeAddList){
                res.wesureTimeAddList.forEach((item,index)=>{
                    let ind = index+=1
                    index -= 1
                    res.wesureTimeAddList[index].whatDay = '第' + ind + '次投保时间'
                })
            }
            
            this.setData({
                addTimeList:res,
                day:res.wesureInfo.wesureTimeDayShift,
                night:res.wesureInfo.wesureTimeNightShift
            })
        })
    },

    onInsureDelete(){
        // deleteInsureEnterprise
        confirm('确认删除').then(()=>{
            deleteInsureEnterprise({
                id:this.data.id,
                insureCompanyType:user.insureCompanyType
            }).then(()=>{
                wx.navigateBack()
                const eventChannel = this.getOpenerEventChannel()
                eventChannel.emit('acceptDataFromOpenedPage',{data:'text'})
                toast('删除成功')
            })
        })
    },
    onConfirmModify(){
        confirm('确认修改').then(()=>{
            if(this.data.day || this.data.night){
                addInsuranceEnterprise({
                    id:this.data.id,
                    wesureTimeDayShiftAudit:this.data.day,
                    wesureTimeNightShiftAudit:this.data.night,
                    wesureTimeAddList:this.data.addTimeList.wesureTimeAddList,
                    insureCompanyType:user.insureCompanyType
                }).then(()=>{
                    wx.navigateBack()
                    const eventChannel = this.getOpenerEventChannel()
                    eventChannel.emit('acceptDataFromOpenedPage')
                    toast('修改成功')
                })
            }else{
                toast('请至少选择一项投保时间')
            }
        })
        
    },
    // 选择白班投保时间
    dayBindTimeChange(event){
        const date = dateFormat('YY-mm-dd',new Date()) + ' ' + event.detail.value + ':00'
        const time =  Date.create(date).getTime() + 1000*60*90
        const delayTime = dateFormat('HH:MM',new Date(time))
        this.setData({
            day:event.detail.value + '-' + delayTime
        })
    },
    // 选择夜班投保时间
    nightBindTimeChange(event){
        const date = dateFormat('YY-mm-dd',new Date()) + ' ' + event.detail.value + ':00'
        const time =  Date.create(date).getTime() + 1000*60*90
        const delayTime = dateFormat('HH:MM',new Date(time))
        console.log(date,time,delayTime)
        this.setData({
            night:event.detail.value + '-' + delayTime
        })
    },
    onGo(){
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-apply-time/insurance-apply-time?id=' + this.data.id + '&companyname=' + this.data.companyName,
        })
    },
    modifyAddTime(event){
        const { index } = event.currentTarget.dataset
        console.log(event,'event')
        const date = dateFormat('YY-mm-dd',new Date()) + ' ' + event.detail.value + ':00'
        const time =  Date.create(date).getTime() + 1000*60*30
        const delayTime = dateFormat('HH:MM',new Date(time))
        console.log(date,time,delayTime)
        this.setData({
            ['addTimeList.wesureTimeAddList['+index+'].wesureTimeAdd']:event.detail.value + '-' + delayTime,
            ['addTimeList.wesureTimeAddList['+index+'].wesureTimeAddAudit']:event.detail.value + '-' + delayTime
        })
    }
})